package com.freshcity.utils
{
	import com.freshcity.core.GlobalVar;
	import com.greensock.loading.LoaderMax;
	import com.maccherone.json.JSON;
	import com.pblabs.debug.Logger;

	public class MessageUtil
	{
		private static var xml:XML=LoaderMax.getContent("configXML");

		public static function decodeWeather(index:int):String
		{
			return xml.weather.it[index];
		}

		public static function decodePeriod(index:int):String
		{
			return xml.period.it[index];
		}

		public static function decodeWater(index:int):String
		{
			return xml.water.it[index];
		}

		public static function decodeLevel(index:int):String
		{
			return index + "级信用";
		}

		public static function decodeAssoLevel(index:int):String
		{
			return xml.asso.it[index];
		}

		public static function decodeFlowerMean(amt:String):String
		{
			return xml.flower.it.(@v == amt);
		}
		
		public static function decodeITask(desc:int, target:String, status:int):String
		{
			return desc + "/" + target + "/" + status;
		}

		public static function getFeijiGuildWords():String
		{
			var len:int=xml.feiji.it.length();
			var index:int=Math.random() * len;
			return xml.feiji.it[index];
		}

		public static function decodeAge(birthYear:String):String
		{
			var start:int=int(birthYear);
			var end:int=(new Date()).fullYear;

			return String(end - start);
		}

		private static function renderWater(index:int):String
		{
			return '<span class="c_tx4">' + xml.water.it[index] + '</span>';
		}
		
		private static function renderFlower(amt:int):String
		{
			var amtStr:String = String(amt);
			return '<span class="c_tx4">' + MessageUtil.decodeFlowerMean(amtStr) + '</span>';
		}		

		public static function friendRank(rank:Array, isOwner:Boolean=false):String
		{
			var result:String='<p id="myStatusUl">';
			var i:int=0;
			var oArray:Array;
			var owner:String=isOwner ? "我" : "她";

			if (rank.length == 0)
			{
				result+="<div>暂时没有发现人帮助" + owner + "照顾这颗树</div>";
			}

			for each (var o:String in rank)
			{
				oArray=o.split(",");
				i++;
				result+="<div>";
				if (oArray[0] == GlobalVar.LOGIN_USER.id)
				{
					result+="".concat("<font color='#ff0000'> ", i, " 我（", oArray[1], "）</font>");
				}
				else
				{
					result+=i + " 用户".concat(oArray[0], " （", oArray[1], "）");
				}
				result+="</div>";
			}
			result+='</p>';
			return result;
		}

		public static function decodeAssoLog(evtlog:String):String
		{
			Logger.debug(null, "decodeAssoLog", evtlog);
			var result:String='<p id="myStatusUl">';
			var logArray:Array=JSON.decode(evtlog) as Array;
			for each (var o:* in logArray)
			{
				var d:String=o.d;
				result+="<div>" + d.substr(5) + "-&gt;";

				switch (o.a)
				{
					case 0:
					{
						result+="我对TA有点好感。";
						break;
					}
					case 1:
					{
						result+="我轻声和TA打了声招呼。";
						break;
					}
					case 2:
					{
						result+="向TA送了一份清水。";
						break;
					}
					case 3:
					{
						result+="我给TA留言了";
						break;
					}
					case 4:
					{
						result+="我和TA在线聊天啦。";
						break;
					}
					case 8:
					{
						result+="我和TA关系提升到" + MessageUtil.decodeAssoLevel(o.t);
						break;
					}
					default:
					{
						break;
					}
				}

				result+="</div>";
			}
			result+='</p>';

			return result;
		}

		public static function decodeUserLog(src:*):*
		{
			Logger.debug(null, "decodeAssoLog", src.userLog);
			var result:String='<p id="myStatusUl">';
			var len:int=0;
			var logArray:Array=JSON.decode(src.userLog) as Array;
			for each (var o:* in logArray)
			{
				var d:String=o.d;
				if (StringUtil.strToDate(src.latestVisit) < StringUtil.strToDate(d))
				{
					len++;
				}
				result+="<div>" + d.substr(5) + "-&gt;";

				switch (o.a)
				{
					case 1:
					{
						result+=''.concat('<a href="event:','u*',o.u, '" class="nickname textoverflow">用户', o.u, '</a>送来了', renderWater(o.t), '。', '<a href="event:','thank*', o.u, '" class="nickname textoverflow">感谢他</a>');
						break;
					}
					case 2:
					{
						result+=''.concat('<a href="event:','u*',o.u, '" class="nickname textoverflow">用户', o.u, '</a>给你留言了。', '<a href="event:','s*', o.u, '" class="nickname textoverflow">查看</a>');
						break;
					}
					case 3:
					{
						result+=''.concat('<a href="event:', 'u*',o.u, '" class="nickname textoverflow">用户', o.u, '</a>正在关注你，TA对你有好感。');
						break;
					}
					case 5:
					{
						result+=''.concat('<a href="event:', 'u*',o.u, '" class="nickname textoverflow">用户', o.u, '(QQ:', o.t, ')</a>获取了你的QQ号码。', '<a href="event:','fthank*', o.u, '" class="nickname textoverflow">回复</a>');
						break;
					}
					case 8:
					{
						result+="太棒了！你的纸飞机被评为本周明星飞机！";
						break;
					}
					case 9:
					{
						result+=''.concat('<a href="event:', 'u*',o.u, '" class="nickname textoverflow">用户', o.u, '</a>向你打了声招呼。');
						break;
					}
					case 10:
					{
						result+=''.concat('<a href="event:', 'u*',o.u, '" class="nickname textoverflow">用户', o.u, '</a>邀请你升级个人信用等级。', '<a href="event:','up*', o.u, '" class="nickname textoverflow">升级</a>');
						break;
					}
					case 11:
					{
						result+=''.concat('<a href="event:', 'u*',o.u, '" class="nickname textoverflow">用户', o.u, '</a>正邀请你栽颗梧桐树，让他有机会表达心意。', '<a href="event:','nt*', o.u, '" class="nickname textoverflow">去栽树</a>');
						break;
					}
					case 12:
					{
						result+=''.concat('<a href="event:', 'u*',o.u, '" class="nickname textoverflow">用户', o.u, '</a>正请求你送些清水，好让她的梧桐树快些长大。', '<a href="event:','u*', o.u, '" class="nickname textoverflow">送水</a>');
						break;
					}
					case 13:
					{
						result+=''.concat('<a href="event:', 'u*',o.u, '" class="nickname textoverflow">用户', o.u, '</a>感谢你送来的清水,她很开心。');
						break;
					}
					case 14:
					{
						var s:String = o.t;
						var loc1:Array = s.split("*");
						var loc2:String = s;
						var loc3:String = "1";
						
						if(loc1){
							loc2 = loc1[0];
							loc3 = loc1[1];
						}
						var loc:int = int(loc3)+1;
						var loc4:String = MessageUtil.decodeAssoLevel(loc);
						
						result+=''.concat('<a href="event:', 'u*',o.u, '" class="nickname textoverflow">用户', o.u, '</a>想和你成为', StringUtil.addColor(loc4,"#009933") ,'，TA送上',loc2,'朵玫瑰,TA想说：',renderFlower(int(loc2)),'。', '<a href="event:','fthank*', o.u, '" class="nickname textoverflow">仅感谢</a>',' | ', '<a href="event:','facp*', o.u, '*', loc, '" class="nickname textoverflow">接受</a>');
						break;
					}
					case 15:
					{
						var loc5:String = o.t;
						if(loc5=='') loc5 = "1";
						var loc6:String = MessageUtil.decodeAssoLevel(int(loc5));
						
						result+=''.concat('<a href="event:', 'u*',o.u, '" class="nickname textoverflow">用户', o.u, '</a>接受了，你们的关系已提升到',StringUtil.addColor(loc6,"#009933"),'。');
						break;
					}
					default:
					{
						break;
					}
				}

				result+="</div>";
			}
			result+='</p>';
			return {log: result, amt: len};
		}

		public static function decodeTreeLog(treelog:String, isOwner:Boolean=false):String
		{
			Logger.debug(null, "decodeTreeLog", treelog);
			var owner:String;

			var result:String='<p id="myStatusUl">';
			var logArray:Array=JSON.decode(treelog) as Array;
			for each (var o:* in logArray)
			{
				owner=(o.u == GlobalVar.LOGIN_USER.id) ? "我" : "她";

				var d:String=o.d;
				result+="<div>" + d.substr(5) + "-&gt;";

				switch (o.a)
				{
					case 0:
					{
						result+=owner + "栽下梧桐树苗。";
						break;
					}
					case 1:
					{
						if (isOwner)
						{
							result+='<a href="event:' + o.u + '" class="nickname textoverflow">用户' + o.u + '</a>送来了' + renderWater(o.t) + '。';
						}
						else
						{
							if (o.u == GlobalVar.LOGIN_USER.id)
							{
								result+="我送给她" + renderWater(o.t) + "。";
							}
							else
							{
								result+="有人送来了" + renderWater(o.t) + "。";
							}
						}
						break;
					}
					case 2:
					{
						result+=owner + '用' + renderWater(o.t) + '浇灌。';
						break;
					}
					case 3:
					{
						result+="终于，梧桐树成熟了。";
						break;
					}
					case 4:
					{
						result+="幸福的日子，" + owner + "为自己许了一个愿望。";
						break;
					}
					default:
					{
						break;
					}
				}

				result+="</div>";
			}
			result+='</p>';

			return result;
		}
	}
}